package order

import (
	"parking-lot-go-admin/controller"
	"parking-lot-go-admin/model"
	"parking-lot-go-admin/model/view"
	"time"

	"github.com/gin-gonic/gin"
)

func List(c *gin.Context) {

	requestDataMap := make(map[string]interface{})
	c.ShouldBindJSON(&requestDataMap)

	where, page, pageSize := controller.WhereStructure(requestDataMap)

	//查询信息
	var dingtalkOrderViewStruct view.DingtalkOrderViewStruct
	var dingtalkOrderViewData []view.DingtalkOrderViewStruct
	model.GetModelData(
		model.Db,
		dingtalkOrderViewStruct.TableName(),
		&dingtalkOrderViewData,
		model.QueryOptions{
			Conditions: where,
			OrderBy:    "order_id desc",
			Page:       page,
			PageSize:   pageSize,
			Debug:      true,
		},
	)

	//数据格式化
	returnData := make([]map[string]interface{}, 0)
	for _, v := range dingtalkOrderViewData {
		returnData = append(returnData, map[string]interface{}{
			"order_car_number":      v.OrderCarNumber,
			"order_car_no_color":    v.OrderCarNoColor,
			"order_car_type":        v.OrderCarType,
			"parking_lot_park_code": v.ParkingLotParkCode,
			"parking_lot_name":      v.ParkingLotName,
			"order_order_no":        v.OrderOrderNo,
			"order_in_time":         isZeroTime(v.OrderInTime),
			"order_out_time":        isZeroTime(v.OrderOutTime),
			"order_status":          v.OrderStatus,
		})
	}

	//总数查询
	count, _ := model.CountModelData(
		model.Db,
		dingtalkOrderViewStruct.TableName(),
		model.QueryOptions{
			Conditions: where,
		},
	)

	controller.Success(c,
		map[string]interface{}{
			"list":        returnData,
			"total":       count,
			"pageSize":    pageSize,
			"currentPage": page,
		})

}

func isZeroTime(inTime time.Time) interface{} {
	var outTime interface{}
	if inTime.IsZero() {
		outTime = ""
	} else {
		outTime = inTime.Format("2006-01-02 15:04:05")
	}
	return outTime
}
